/**
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with this
 * work for additional information regarding copyright ownership.  The ASF
 * licenses this file to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package org.apache.hadoop.fs.obs;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import com.obs.services.ObsClient;
import com.obs.services.model.ListBucketsRequest;
import com.obs.services.model.ObsBucket;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.obs.contract.OBSContract;
import org.junit.Assume;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Rule;
import org.junit.Test;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.List;

public class ITestOBSFileSystem {

    private static final int EXPECTED_PORT = -1;

    private static final String EXPECTED_SCHEMA = "obs";

    private Configuration conf;

    @Rule
    public OBSTestRule testRule = new OBSTestRule();

    @BeforeClass
    public static void skipTestCheck() {
        Assume.assumeTrue(OBSContract.isContractTestEnabled());
    }

    @Before
    public void setUp() throws Exception {
        conf = OBSContract.getConfiguration(null);
    }

    @Test
    // 通过configuration初始化FS，功能正常
    public void testInitialization() throws IOException {
        String bucketName = conf.get(OBSTestConstants.TEST_FS_OBS_NAME);
        initializationTest(bucketName);
    }

    @Test
    public void testInitializeNotExistsBucket() throws URISyntaxException, IOException {
        String initializationUri = conf.get(OBSTestConstants.TEST_FS_OBS_NAME);
        URI uri = new URI(initializationUri);
        String scheme = uri.getScheme();
        String bucketName = uri.getAuthority();
        while (true) {
            bucketName = String.format("%s-not-exists", bucketName);
            OBSFileSystem fs = new OBSFileSystem();
            try {
                initializationUri = String.format("%s://%s", scheme, bucketName);
                fs.initialize(URI.create(initializationUri), conf);
            } catch (FileNotFoundException e) {
                assertEquals("Bucket " + bucketName + " does not exist", e.getMessage());
                return;
            }
            List<ObsBucket> buckets = getBucketList(fs.getObsClient());
            boolean isBucketInList = false;
            for (ObsBucket bucket : buckets) {
                if (bucket.getBucketName().equals(fs.getBucket())) {
                    isBucketInList = true;
                    break;
                }
            }
            if (isBucketInList) {
                continue;
            }
            assertTrue("not in bucket list, but initalize success", false);
        }
    }

    private void initializationTest(String initializationUri)
        throws IOException {
        OBSFileSystem fs = new OBSFileSystem();
        fs.initialize(URI.create(initializationUri), conf);
        ObsClient obsTest = fs.getObsClient();
        List<ObsBucket> buckets = getBucketList(obsTest);

        boolean isBucketInList = false;
        for (ObsBucket bucket : buckets) {
            if (bucket.getBucketName().equals(fs.getBucket())) {
                isBucketInList = true;
                break;
            }
        }

        URI EXPECTED_URI = URI.create(conf.get(OBSTestConstants.TEST_FS_OBS_NAME));
        assertTrue(isBucketInList);
        assertEquals(EXPECTED_SCHEMA, fs.getScheme());
        assertEquals(EXPECTED_URI, fs.getUri());
        assertEquals(EXPECTED_PORT, fs.getDefaultPort());
    }

    private List<ObsBucket> getBucketList(ObsClient obsTest) {
        ListBucketsRequest request = new ListBucketsRequest();
        request.setQueryLocation(true);
        List<ObsBucket> buckets = obsTest.listBuckets(request);
        return buckets;
    }
}
